package com.tianya.portal.blog.common;

import com.tianya.common.web.core.domain.TreeNode;
import com.tianya.common.web.utils.TreeBuild;
import com.tianya.portal.blog.constant.BlogConstant;
import com.tianya.portal.blog.domain.BlogNoteMenu;
import com.tianya.portal.blog.mapper.BlogNoteMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 初始化缓存配置
 * 注：项目一启动缓存生效
 */
@Component
//最外层有一个后执行的 后加的需要小于1000
@Order(value = 900)

public class CacheConfiguration implements CommandLineRunner {
    @Autowired
    private BlogNoteMenuMapper blogNoteMenuMapper;

    public void cacheNoteMenu() {
        BlogNoteMenu noteMenu = new BlogNoteMenu();
        List<BlogNoteMenu> noteMenuList = blogNoteMenuMapper.selectBlogNoteMenuList(noteMenu);
        List<TreeNode> treeNodeList = new ArrayList<>();
        noteMenuList.stream().forEach(e -> {
            treeNodeList.add(new TreeNode(e.getMenuId(), e.getParentId(), e.getMenuName()));
        });
        // 创建树形结构（数据集合作为参数）
        TreeBuild treeBuild = new TreeBuild(treeNodeList);
        // 原查询结果转换树形结构
        BlogConstant.menuTreeSelectList = treeBuild.buildTree();
    }

    /**
     * 获取配置表中的信息
     */
    @Override
    public void run(String... args) throws Exception {
        cacheNoteMenu();
    }
}
